package com.ruixiang.study;

import org.junit.Test;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author ruixinag
 * @description 泛型demo
 * @create Created in 2022/1/21 10:56
 */
public class GenericDemo {
    public static void main(String[] args) throws Exception {
        List<String> stringList = new ArrayList<>(Arrays.asList("a", "b", "c"));
        print(stringList);

        List<Integer> integerList = new ArrayList<>(Arrays.asList(1, 2, 3));
        print(integerList);
    }

    public static void print(List list) {
        for (Object o : list) {
            System.out.println(o);
        }
    }


    @Test
    public void test1() {
        ArrayList<Integer> integerArrayList = new ArrayList<>();
        integerArrayList.add(1);
        integerArrayList.add(2);
        // 泛型的类型向上强制
        List<? extends Number> longArrayList = new ArrayList<>();
        System.out.println(longArrayList.get(1));
        List<? extends Number> doubleArrayList = new ArrayList<Integer>();

    }

    @Test
    public void test2() {
        List<? super Number> numberArrayList = new ArrayList<>();
        numberArrayList.add(1);
        numberArrayList.add(10L);
        numberArrayList.add(1.2);

        Number object = (Number) numberArrayList.get(0);
        System.out.println("object = " + object);

    }


}
